﻿Option Explicit On
Option Strict On

''' <summary>
''' Formulario Buscar: Para buscar en la BBDD
''' </summary>
''' <author>David Martínez</author>

Public Class Buscar
    Private listaCitas As List(Of Cita)
    Private listaFacturas As List(Of Factura)
    Private listaClientes As List(Of Cliente)
    Private miBBDD As BBDD

    Private Sub Buscar_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        miBBDD = New BBDD
    End Sub

    Private Sub BuscarCliente(sender As System.Object, e As System.EventArgs) Handles btBuscarCliente.Click
        dgvClientesBuscar.Rows.Clear()
        miBBDD.Conectar()
        listaClientes = miBBDD.ListaClientes("Select * from cliente where nombre = '" & tbBuscarCliente.Text & "' or apellidos = '" & tbBuscarCliente.Text & "' or dni = '" & tbBuscarCliente.Text & "'")
        miBBDD.Desconectar()
        For Each clienteActual As Cliente In listaClientes
            dgvClientesBuscar.Rows.Add(clienteActual.Nombre, clienteActual.Apellidos, clienteActual.DNI)
        Next

    End Sub

    Private Sub BuscarCita(sender As System.Object, e As System.EventArgs) Handles btBuscarCita.Click
        Dim listaConceptos As List(Of Servicio)
        Dim conceptos As String = ""

        dgvCitasBuscar.Rows.Clear()
        miBBDD.Conectar()
        listaCitas = miBBDD.ListaCitas("Select * from cita where cod_empleado = " & tbBuscarCita.Text & " or cod_cliente = " & tbBuscarCita.Text)
        miBBDD.Desconectar()
        For Each citaActual As Cita In listaCitas
            conceptos = ""
            miBBDD.Conectar()
            listaConceptos = miBBDD.ListaServicios("Select * from servicios where cod in (Select cod_servicio from concepto where cod_cita = " & CStr(citaActual.Codigo) & ")")
            miBBDD.Desconectar()
            For Each servicioUnico As Servicio In listaConceptos
                conceptos = conceptos & servicioUnico.Nombre & ","
            Next
            dgvCitasBuscar.Rows.Add(citaActual.CodigoCliente, citaActual.CodigoUsuario, citaActual.Fecha.ToString("hh:mm"), citaActual.Fecha.ToString("dd/MM"), conceptos)
        Next
    End Sub

    Private Sub BuscarFactura(sender As System.Object, e As System.EventArgs) Handles btBuscarFactura.Click
        miBBDD.Conectar()
        listaFacturas = miBBDD.ListaFacturas("Select * from factura where cod_cita = " & tbFacturaBuscar.Text)
        miBBDD.Desconectar()
        For Each facturaACtual As Factura In listaFacturas
            dgvFacturasBuscar.Rows.Add(facturaACtual.CodigoCita, facturaACtual.Fecha.ToString("dd/MM"), facturaACtual.Fecha.ToString("hh:mm"), facturaACtual.Cobro, facturaACtual.Descuento)
        Next
    End Sub
End Class